<template>
  <div class="profit">
    <!-- 激活矿机 -->
    <div>
      <div class="flex flex-center mt-20 mb-10">
        <div class="title title--bg">{{ $t('激活矿机') }}</div>
      </div>

      <div class="ta-center">
        <van-button
          class="btn"
          type="primary"
          plain
          @click="router.push('/miner')"
        >
          {{ $t('激活矿机') }}
        </van-button>
      </div>
    </div>

    <!-- 收益提取 -->
    <div>
      <div class="flex flex-center mt-20 mb-10">
        <div class="title title--bg">{{ $t('收益提取') }}</div>
      </div>

      <div v-if="statistics.length">
        <ul class="data-list">
          <li
            class="box pt-15 pb-15 mt-10"
            v-for="(v, i) in statistics"
            :key="i"
          >
            <div class="flex flex-center cl-success fz-15 mb-10">
              <img
                class="mr-5"
                style="width: 16px"
                src="../assets/img/amount.png"
              />
              <div>{{ $t('总资产') }}：</div>
              <div>{{ v.amount?.toFixed(6) || 0 }} U</div>
            </div>

            <div class="flex flex-rb ta-center lh-2">
              <div style="width: 33.33%">
                <div>{{ $t('激活矿机收益') }}</div>
                <div class="cl-success mb-10">
                  {{ v.profit_pa?.toFixed(6) || 0 }} U
                </div>
                <van-button
                  class="btn"
                  type="primary"
                  plain
                  @click="addWithdraw(v.id, 0)"
                >
                  {{ $t('提取') }}
                </van-button>
              </div>

              <div style="width: 33.33%">
                <div>{{ $t('购物券收益') }}</div>
                <div class="cl-success mb-10">
                  {{ v.profit_pa?.toFixed(6) || 0 }} U
                </div>
                <van-button
                  class="btn"
                  type="primary"
                  plain
                  @click="addWithdraw(v.id, 2)"
                >
                  {{ $t('提取') }}
                </van-button>
              </div>

              <div style="width: 33.33%">
                <div>{{ $t('提取收益') }}</div>
                <div class="cl-success mb-10">
                  {{ v.profit_pb?.toFixed(6) || 0 }} U
                </div>
                <van-button
                  class="btn"
                  type="primary"
                  plain
                  @click="addWithdraw(v.id, 1)"
                >
                  {{ $t('提取') }}
                </van-button>
              </div>
            </div>
          </li>
        </ul>

        <!-- 分页 -->
        <van-pagination
          class="mt-20"
          mode="simple"
          :prev-text="$t('上一页')"
          :next-text="$t('下一页')"
          :total-items="total1"
          :items-per-page="size1"
          @change="getTotal"
          v-model="page1"
        ></van-pagination>
      </div>

      <van-empty :description="$t('暂无数据')" v-else></van-empty>
    </div>

    <!-- 股东收益 -->
    <div>
      <div class="flex flex-center mt-20 mb-10">
        <div class="title title--bg">{{ $t('股东收益') }}</div>
      </div>

      <ul class="data-list">
        <li class="box pt-15 pb-15 mt-10">
          <div class="flex flex-center cl-success fz-15 mb-10">
            <div>{{ $t('股东奖池总金额') }}：</div>
            <div>{{ gudongWithdraw.total_serves || 0 }} U</div>
          </div>

          <div class="flex flex-center cl-success fz-15 mb-10">
            <div>{{ $t('股东身份') }}：</div>
            <div>{{ gudongWithdraw.status == 1 ? $t('是') : $t('否') }}</div>
          </div>

          <div class="flex flex-rb ta-center lh-2">
            <div style="width: 50%">
              <div>{{ $t('可提取收益') }}</div>
              <div class="cl-success mb-10">
                {{ gudongWithdraw.bonus_serves || 0 }} U
              </div>
              <van-button
                class="btn"
                type="primary"
                plain
                @click="addGudongWithdraw"
              >
                {{ $t('提取') }}
              </van-button>
            </div>

            <div style="width: 45%">
              <div>{{ $t('已提取收益') }}</div>
              <div class="cl-success mb-10">
                {{ gudongWithdraw.bonus_cash || 0 }} U
              </div>
            </div>
          </div>
        </li>
      </ul>
    </div>

    <!-- 股东提现详情 -->
    <div>
      <div class="flex flex-center mt-20 mb-10">
        <div class="title title--bg">{{ $t('股东提现详情') }}</div>
      </div>

      <div>
        <div v-if="godongWithdraws.length">
          <ul class="data-list">
            <li
              class="box flex lh-2 mt-10"
              v-for="(v, i) in godongWithdraws"
              :key="i"
            >
              <div class="item">
                <span class="label">{{ $t('提现金额') }}</span>
                <span class="value">{{ v.bonus_cash }}</span>
              </div>
              <div class="item">
                <span class="label">{{ $t('提现时间') }}</span>
                <span class="value">{{ formatDateTime(v.paytime) }}</span>
              </div>
              <div class="item" style="width: 100%" v-if="v.hash">
                <span class="label">{{ $t('哈希') }}</span>
                <span class="value click" @click="copyString(v.hash)">
                  {{ v.hash }}
                </span>
              </div>
            </li>
          </ul>

          <!-- 分页 -->
          <van-pagination
            class="mt-10"
            mode="simple"
            :prev-text="$t('上一页')"
            :next-text="$t('下一页')"
            :total-items="total4"
            :items-per-page="size4"
            @change="getGudongWithdraws"
            v-model="page4"
          ></van-pagination>
        </div>

        <van-empty :description="$t('暂无数据')" v-else></van-empty>
      </div>
    </div>

    <!-- 收益详情 -->
    <div>
      <div class="flex flex-center mt-20 mb-10">
        <div class="title title--bg">{{ $t('收益详情') }}</div>
      </div>

      <div class="flex flex-rb mb-20">
        <van-button
          class="btn"
          type="primary"
          v-for="(v, i) in tabs"
          :key="i"
          :plain="tabIndex !== i"
          @click=";(tabIndex = i), (page2 = 1), getDetails()"
        >
          {{ v }}
        </van-button>
      </div>

      <div v-if="details.length">
        <!-- 用户理财 -->
        <ul class="data-list" v-if="tabIndex === 0">
          <li class="flex box lh-2 mt-10" v-for="(v, i) in details" :key="i">
            <!-- <div class="item">
              <span class="label">用户</span>
              <span class="value">{{ v.user_id }}</span>
            </div> -->
            <div class="item">
              <span class="label">{{ $t('总质押额') }}</span>
              <span class="value">{{ v.amount_all }}</span>
            </div>
            <!-- <div class="item">
              <span class="label">有效质押额</span>
              <span class="value">{{ v.amount_real }}</span>
            </div> -->
            <div class="item">
              <span class="label">{{ $t('获取收益倍数') }}</span>
              <span class="value">{{ v.amount_times }}</span>
            </div>
            <div class="item">
              <span class="label">{{ $t('退场额') }}</span>
              <span class="value">{{ v.amount_exit }}</span>
            </div>
            <div class="item">
              <span class="label">{{ $t('日化收益率') }}(%)</span>
              <span class="value">{{ (v.rate_year / 365).toFixed(2) }}</span>
            </div>
            <div class="item">
              <span class="label">{{ $t('阶段收益') }}</span>
              <span class="value">{{ v.pr }}</span>
            </div>
            <!-- <div class="item">
              <span class="label">收益A</span>
              <span class="value">{{ v.pa }}</span>
            </div>
            <div class="item">
              <span class="label">收益B</span>
              <span class="value">{{ v.pb }}</span>
            </div> -->
            <div class="item">
              <span class="label">{{ $t('期次') }}</span>
              <span class="value">{{ v.batch }}</span>
            </div>
            <!-- <div class="item">
              <span class="label">创建时间</span>
              <span class="value">{{ formatDateTime(v.create_time) }}</span>
            </div> -->
            <div class="item">
              <span class="label">{{ $t('更新时间') }}</span>
              <span class="value">{{ formatDateTime(v.update_time) }}</span>
            </div>
          </li>
        </ul>

        <!-- 客户分红 -->
        <ul class="data-list" v-if="tabIndex === 1">
          <li class="flex box lh-2 mt-10" v-for="(v, i) in details" :key="i">
            <!-- <div class="item">
              <span class="label">用户</span>
              <span class="value">{{ v.user_id }}</span>
            </div> -->
            <div class="item" style="width: 100%">
              <span class="label">{{ $t('有效客户数') }}</span>
              <span class="value">{{ v.cnt_sons }}</span>
            </div>
            <div class="item" style="width: 100%">
              <span class="label">{{ $t('有效客户质押总额') }}</span>
              <span class="value">{{ v.total_amount }}</span>
            </div>
            <div class="item" style="width: 100%">
              <span class="label">{{ $t('客户当期理财收益总额') }}</span>
              <span class="value">{{ v.total_profit }}</span>
            </div>
            <div class="item" style="width: 100%">
              <span class="label">{{ $t('获得客户比例') }}(%)</span>
              <span class="value">{{ v.rate }}</span>
            </div>
            <div class="item">
              <span class="label">{{ $t('分红') }}</span>
              <span class="value">{{ v.bonus }}</span>
            </div>
            <div class="item">
              <span class="label">{{ $t('期次') }}</span>
              <span class="value">{{ v.batch }}</span>
            </div>
            <!-- <div class="item">
              <span class="label">创建时间</span>
              <span class="value">{{ formatDateTime(v.create_time) }}</span>
            </div> -->
            <div class="item">
              <span class="label">{{ $t('更新时间') }}</span>
              <span class="value">{{ formatDateTime(v.update_time) }}</span>
            </div>
          </li>
        </ul>

        <!-- 团队奖励 -->
        <ul class="data-list" v-if="tabIndex === 2">
          <li class="flex box lh-2 mt-10" v-for="(v, i) in details" :key="i">
            <!-- <div class="item">
              <span class="label">用户</span>
              <span class="value">{{ v.user_id }}</span>
            </div> -->
            <div class="item">
              <span class="label">{{ $t('有效客户数') }}</span>
              <span class="value">{{ v.cnt_sons }}</span>
            </div>
            <div class="item">
              <span class="label">{{ $t('等级') }}</span>
              <span class="value">{{ v.level }}</span>
            </div>
            <!--            <div class="item">
              <span class="label">所有下级</span>
              <span class="value">{{ v.son_ids }}</span>
            </div> -->
            <div class="item">
              <span class="label">{{ $t('伞下总业绩') }}</span>
              <span class="value">{{ v.total_all }}</span>
            </div>
            <div class="item">
              <span class="label">{{ $t('小市场总业绩') }}</span>
              <span class="value">{{ v.total_small }}</span>
            </div>
            <!-- <div class="item">
              <span class="label">大市场下级</span>
              <span class="value">{{ v.big_id }}</span>
            </div> -->
            <div class="item">
              <span class="label">{{ $t('大市场达到等级') }}</span>
              <span class="value">{{ v.big_level }}</span>
            </div>

            <div class="item">
              <span class="label">{{ $t('大市场总业绩') }}</span>
              <span class="value">{{ v.total_big }}</span>
            </div>
            <div class="item">
              <span class="label">{{ $t('获得团队比例') }}(%)</span>
              <span class="value">{{ v.rate }}</span>
            </div>
            <div class="item" style="width: 100%">
              <span class="label">{{ $t('团队理财收益总额') }}</span>
              <span class="value">{{ v.total_profit }}</span>
            </div>
            <div class="item">
              <span class="label">{{ $t('奖励') }}</span>
              <span class="value">{{ v.rewards }}</span>
            </div>
            <div class="item">
              <span class="label">{{ $t('期次') }}</span>
              <span class="value">{{ v.batch }}</span>
            </div>
            <!-- <div class="item">
              <span class="label">创建时间</span>
              <span class="value">{{ formatDateTime(v.create_time) }}</span>
            </div> -->
            <div class="item">
              <span class="label">{{ $t('更新时间') }}</span>
              <span class="value">{{ formatDateTime(v.update_time) }}</span>
            </div>
          </li>
        </ul>

        <!-- 分页 -->
        <van-pagination
          class="mt-20"
          mode="simple"
          :prev-text="$t('上一页')"
          :next-text="$t('下一页')"
          :total-items="total2"
          :items-per-page="size2"
          @change="getDetails"
          v-model="page2"
        ></van-pagination>
      </div>

      <van-empty :description="$t('暂无数据')" v-else></van-empty>
    </div>

    <!-- 提现单 -->
    <div>
      <div class="flex flex-center mt-20 mb-10">
        <div class="title title--bg">{{ $t('提现单') }}</div>
      </div>

      <div>
        <div v-if="withdraws.length">
          <ul class="data-list">
            <li
              class="box flex lh-2 mt-10"
              v-for="(v, i) in withdraws"
              :key="i"
            >
              <div class="item">
                <span class="label">{{ $t('提现金额') }}</span>
                <span class="value">{{ v.amount }}</span>
              </div>
              <div class="item">
                <span class="label">{{ $t('提现时间') }}</span>
                <span class="value">{{ formatDateTime(v.create_time) }}</span>
              </div>
              <div class="item" style="width: 100%" v-if="v.hash">
                <span class="label">{{ $t('哈希') }}</span>
                <span class="value click" @click="copyString(v.hash)">
                  {{ v.hash }}
                </span>
              </div>
            </li>
          </ul>

          <!-- 分页 -->
          <van-pagination
            class="mt-10"
            mode="simple"
            :prev-text="$t('上一页')"
            :next-text="$t('下一页')"
            :total-items="total3"
            :items-per-page="size3"
            @change="getWithdraws"
            v-model="page3"
          ></van-pagination>
        </div>

        <van-empty :description="$t('暂无数据')" v-else></van-empty>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { useI18n } from 'vue-i18n'
import { showToast, formatDateTime, copyString } from 'js/utils'
import apis from 'js/apis'

import { showConfirmDialog } from 'vant'
import 'vant/es/dialog/style'

const router = useRouter()
const { t } = useI18n()

// 类型
const tabs = computed(() => [t('用户理财'), t('客户分红'), t('团队奖励')])
const tabIndex = ref(0)

// 收益提取
const statistics = ref([])
const page1 = ref(1)
const size1 = ref(2)
const total1 = ref(0)

// 股东收益
const gudongWithdraw = ref({})

// 股东收益详情
const godongWithdraws = ref([])
const page4 = ref(1)
const size4 = ref(5)
const total4 = ref(0)

// 收益详情
const details = ref([])
const page2 = ref(1)
const size2 = ref(5)
const total2 = ref(0)

// 提现记录
const withdraws = ref([])
const page3 = ref(1)
const size3 = ref(5)
const total3 = ref(0)

getTotal()
getGudongWithdraw()
getGudongWithdraws()
getDetails()
getWithdraws()

// 获取总额
async function getTotal() {
  const params = {
    page: page1.value,
    limit: size1.value,
  }

  const { code, data } = await apis.pledge.getDetails(params)

  if (code === 1) {
    const records = data.data || []

    records.forEach((v) => {
      v.amount = (v.profit_pa || 0) + (v.profit_pb || 0)
    })

    statistics.value = records
    total1.value = data.count || 0
  }
}

// 获取股东收益
async function getGudongWithdraw() {
  const { code, data } = await apis.profit.getGudongWithdraw()

  if (code === 1) {
    gudongWithdraw.value = data || {}
  }
}

// 提取股东收益
async function addGudongWithdraw() {
  const { code } = await apis.profit.addGudongWithdraw(params)

  if (code === 1) {
    showToast(t('操作成功'))
    setTimeout(() => {
      page4.value = 1
      getGudongWithdraw()
      getGudongWithdraws()
    }, 2000)
  }
}

// 获取提现记录
async function getGudongWithdraws() {
  const params = {
    page: page4.value,
    limit: size4.value,
  }

  const { code, data } = await apis.profit.getGudongWithdraws(params)

  if (code === 1) {
    godongWithdraws.value = data.data || []
    total4.value = data.count || 0
  }
}

// 获取收益详情
async function getDetails() {
  const params = {
    page: page2.value,
    limit: size2.value,
  }

  const { code, data } =
    tabIndex.value === 0
      ? await apis.profit.getProfits(params)
      : tabIndex.value === 1
      ? await apis.profit.getAgents(params)
      : await apis.profit.getTeams(params)

  if (code === 1) {
    details.value = data.data || []
    total2.value = data.count || 0
  }
}

// 获取提现记录
async function getWithdraws() {
  const params = {
    page: page3.value,
    limit: size3.value,
  }

  const { code, data } = await apis.profit.getWithdraws(params)

  if (code === 1) {
    withdraws.value = data.data || []
    total3.value = data.count || 0
  }
}

// 显示提现选择
function showWithdrawChoose() {
  showConfirmDialog({
    title: '请选择提现金额',
    cancelButtonText: '最小提现金额',
    confirmButtonText: '最大提现金额',
  })
    .then(() => {
      addWithdraw('max')
    })
    .catch(() => {
      addWithdraw('min')
    })
}

// 提现
async function addWithdraw(id = 0, mode = 0) {
  // const profit80 = store.config.find((v) => v.name === 'profit80')
  // const min = profit80.value['min']
  // const max = profit80.value['max']
  // const total = statistics.value.profit_pb.toFixed(6)

  // if (total < min) {
  //   return showToast('可提取金额小于最小提取金额')
  // }

  // let amount = 0

  // if (type === 'max') {
  //   if (total > max) {
  //     amount = max - (max % min).toFixed(6)
  //   } else {
  //     amount = total - (total % min).toFixed(6)
  //   }
  // } else {
  //   amount = min
  // }

  // const params = {
  //   amount,
  //   mode: 1,
  // }

  const params = {
    id,
    mode,
  }

  const { code } = await apis.profit.addWithdraw(params)

  if (code === 1) {
    showToast(t('操作成功'))

    setTimeout(() => {
      getTotal()
      getDetails()
    }, 2000)
  }
}
</script>

<style lang="scss" scoped></style>
